
#ifndef _DEBUG_H_
#define _DEBUG_H_

#include <stdarg.h>
#include <stdio.h>
#include <stdint.h>
#include <compile.h>
#include "./SystemView/SEGGER_RTT.h"
//#include "bsp.h"

//ASCII控制码
#define LOG_CTRL(ctrl)							LOG("\033[%s", ctrl)	//ASCII控制码
#define LOG_CTRL_CLOSE							"0m"										//关闭所有属性
#define LOG_CTRL_HIGHLIGHT						"1m"										//设置高亮
#define LOG_CTRL_UNDERLINE						"4m"										//下划线 强调
#define LOG_CTRL_BLANK							"5m"										//闪烁
#define LOG_CTRL_COUNTERVAIL					"7m"										//反显

//背景颜色
#define LOG_BC_BLACK							40
#define LOG_BC_RED								41
#define LOG_BC_GREEN							42
#define LOG_BC_YELLOW							43
#define LOG_BC_BLUE								44
#define LOG_BC_PURPLE							45
#define LOG_BC_DGREEN							46
#define LOG_BC_WHITE							47
//字体颜色
#define LOG_FC_BLACK							30//黑 
#define LOG_FC_RED								31//红 
#define LOG_FC_GREEN							32//绿 
#define LOG_FC_YELLOW							33//黄 
#define LOG_FC_BLUE								34//蓝色 
#define LOG_FC_PURPLE							35//紫色 
#define LOG_FC_DGREEN							36//深绿 
#define LOG_FC_WHITE							37//白色

#ifndef NDEBUG
#define LOG(...)								SEGGER_RTT_FreeRTOS_printf(0, __VA_ARGS__)
#else
#define LOG(...)								((void)0)
#endif

#define COLOR_LOG(bc, fc, M, ...)				LOG("\033[%d;%dm%s(%u) %s:  " M "\r\n", bc, fc, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
#define __COLOR_LOG(bc, fc, M, ...)				LOG("\033[%d;%dm" M "\r\n", bc, fc, __VA_ARGS__)
//#define _COLOR_LOG(bc, fc, M, ...)				LOG("\033[%d;%dm" "(%04u-%02u-%02u %02u:%02u:%02u) %s:  " M "\r\n", bc, fc, log_time_get()->y, 	\
																															log_time_get()->_m, \
																															log_time_get()->d, 	\
																															log_time_get()->h, 	\
																															log_time_get()->m, 	\
																															log_time_get()->s, __FUNCTION__, __VA_ARGS__)

#ifndef NDEBUG

#define LOG_D(...)								COLOR_LOG(LOG_BC_BLACK, LOG_FC_WHITE,  __VA_ARGS__, "")
#define LOG_I(...)								COLOR_LOG(LOG_BC_BLACK, LOG_FC_GREEN,  __VA_ARGS__, "")
#define LOG_W(...)								COLOR_LOG(LOG_BC_BLACK, LOG_FC_YELLOW, __VA_ARGS__, "")
#define LOG_E(...)								COLOR_LOG(LOG_BC_BLACK, LOG_FC_RED	 , __VA_ARGS__, "")
#define LOG_WRITE(...)							LOG(__VA_ARGS__)
#define LOG_B(str, buf, len, line) 				log_buff_print(str, buf, len, line, __FUNCTION__)
#define DBG_FUNC()

#else

#define LOG_D(...)
#define LOG_I(...)
#define LOG_W(...)
#define LOG_E(...)
#define LOG_WRITE(...)
#define LOG_B(...)
#define DBG_FUNC()

#endif

typedef struct{
	uint16_t 	y;
	uint8_t		_m;
	uint8_t		d;
	uint8_t		h;
	uint8_t 	m;
	uint8_t 	s;
}log_time_t;


void DebugInit(void);
log_time_t *log_time_get(void);

int SEGGER_RTT_FreeRTOS_printf(unsigned BufferIndex, const char * sFormat, ...);
void log_buff_print(const char *str, const void *buf, uint32_t len, uint8_t line, const char *fun);

#endif

